package com.xywy.fwc;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.provider.ContactsContract.CommonDataKinds.Relation;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.umeng.analytics.MobclickAgent;
import com.xywy.R;
import com.xywy.common.util.FileUtil;
import com.xywy.common.util.ImageLoaderUtil;
import com.xywy.common.widget.MyProgress;
import com.xywy.fwc.bean.FwcHomeData;
import com.xywy.fwc.bean.FwcUser;
import com.xywy.fwc.net.DataCallBack;
import com.xywy.fwc.net.Request;
import com.xywy.fwc.net.UserPaser;
import com.xywy.me.bean.UserKeeper;
/**
 * 服务商详情
 * @author wangwenlong
 *
 */
public class FwcUserInfo extends Activity implements OnClickListener{
	public static  final String infoUrl = "http://api.yun.xywy.com/index.php/server/api/service_provider/introduce/";
    
	private Button bt_title_back;
	private TextView tv_title_back;
	private TextView tv_title;
	private TextView functionTv;
	private TextView serviceTv;
	private ImageView userIv;
	private TextView name,from;
	private FwcUser user;
	
	public static final int  SUCCESS = 0;
	public static final int  FAILED = 1;
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
    	// TODO Auto-generated method stub
    	super.onCreate(savedInstanceState);
    	requestWindowFeature(Window.FEATURE_NO_TITLE);
    	setContentView(R.layout.fwc_user_layout);
    	init();
    	initView();
    	loadData();
    }
    
	
   
	private SharedPreferences sp;
	private String userid;
	private String mSign;
	private FwcHomeData mData;

	private MyProgress progress;

	private void initView() {
		bt_title_back = (Button) findViewById(R.id.bt_title_back);	
		tv_title_back =  (TextView) findViewById(R.id.tv_title_back);
		tv_title = (TextView) findViewById(R.id.tv_title);
		functionTv = (TextView) findViewById(R.id.functionTv);
		serviceTv = (TextView) findViewById(R.id.serviceTv);
		userIv = (ImageView) findViewById(R.id.me_cover_user_photo);
		name =(TextView) findViewById(R.id.name);
		from = (TextView) findViewById(R.id.from);
		
		tv_title.setText(mData.getName());
		bt_title_back.setOnClickListener(this);
		tv_title_back.setOnClickListener(this);
	}

	private void init(){
		sp = getSharedPreferences("config",
				Context.MODE_PRIVATE);
//		userid = sp.getString("userid", "");
		userid = UserKeeper.getCurrentUser(this).getUserid();
		mSign = sp.getString(userid+"sign", "");
		Intent intent = getIntent();
		mData = (FwcHomeData) intent.getSerializableExtra("data");
		progress = new MyProgress(this,30000);
    }
	
	 private void loadData() {
			// TODO Auto-generated method stub
	    	progress.start();
			Request.requestData(new UserPaser(), callback, infoUrl+mData.getId()+"/"+mSign);
		}
	    
	    DataCallBack<FwcUser> callback = new DataCallBack<FwcUser>() {
			
			@Override
			public void dataCall(FwcUser t) {
				// TODO Auto-generated method stub
				user = t;
				if(t!= null){
					FileUtil.saveObject(userid+mData.getId()+FileUtil.FWC_USER, user);
					handler.sendEmptyMessage(SUCCESS);
				}else {
					handler.sendEmptyMessage(FAILED);
				}
			}
		};

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.bt_title_back:
		case R.id.tv_title_back:
			finish();
			break;

		default:
			break;
		}
	}
	Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			if(progress != null){
				progress.stop();
			}
			switch (msg.what) {
			case SUCCESS:
				if(user != null && user.getName() != null)
				setInfo();
				break;
			case FAILED:
				Toast.makeText(FwcUserInfo.this, getString(R.string.common_net_fail), 1).show();
				user = (FwcUser) FileUtil.getObject(userid+mData.getId()+FileUtil.FWC_USER);
				if(user != null && user.getName() != null)
				setInfo();
				break;

			default:
				break;
			}
		}
	};
	
	private void setInfo() {
		// TODO Auto-generated method stub
		name.setText(user.getName());
		from.setText("服务商 :  "+user.getFrom());
		functionTv.setText(user.getIntroduce());
		String privilege[] = user.getPrivilege().split("\n");
		StringBuffer sb = new StringBuffer();
		for(int i=0;i<privilege.length;i++){
			sb.append("●  ")
			.append(privilege[i])
			.append("\n");
		}
		serviceTv.setText(sb.toString());
		ImageLoaderUtil.displayImage(user.getHeadimg(), userIv);
	};
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Stattistics();
	}
    
	
	/**
	 * 友盟统计收藏 帮助
	 */
	public void Stattistics(){
		Map<String, String> params = new HashMap<String, String>();
			params.put("title","服务窗用户中心"); //代表来源
			MobclickAgent.onEvent(this, "Fwc_001", params);
	}
}
